package com.example.springboot.service.impl;

import cn.hutool.core.date.DateUtil;
import com.example.springboot.domain.Blog;
import com.example.springboot.domain.Like;
import com.example.springboot.domain.User;
import com.example.springboot.enums.LikesModuleEnum;
import com.example.springboot.mapper.BlogMapper;
import com.example.springboot.service.BlogService;
import com.example.springboot.service.CollectService;
import com.example.springboot.service.LikeService;
import com.example.springboot.service.UserService;
import com.example.springboot.utils.TokenUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;

@Service
public class BlogServiceImpl implements BlogService {

    @Autowired
    private BlogMapper blogMapper;
    @Autowired
    private UserService userService;
    @Autowired
    private LikeService likeService;
    @Autowired
    private CollectService collectService;

//    添加博客
    @Override
    public void add(Blog blog) {
        blog.setDate(DateUtil.today());
        User user = TokenUtils.getCurrentUser();
        blog.setUserId(user.getUserid());
        blogMapper.insert(blog);
    }

//    删除博客
    @Override
    public void deleteById(Integer id) {
        blogMapper.deleteById(id);
    }

//    批量删除
    @Override
    public void deleteBatch(List<Integer> ids) {
        for(Integer id:ids){
            blogMapper.deleteById(id);
        }
    }

//    修改博客
    @Override
    public void updateById(Blog blog) {
        blogMapper.updateById(blog);
    }

//    根据博客ID查找多个内容
    @Override
    public Blog selectById(Integer id) {
        Blog blog = blogMapper.selectById(id);
        User user = userService.selectById(blog.getUserId());
        List<Blog> userBlogList = blogMapper.selectUserBlog(user.getUserid());
        user.setBlogCount(userBlogList.size());

//        当前用户的点赞量和收藏量
        int userLikesCount = 0;
        int userCollectCount = 0;
        for(Blog b: userBlogList){
            Integer fid = b.getId();
            int likesCount = likeService.selectByFidAndModule(fid, LikesModuleEnum.BLOG.getValue());
            userLikesCount += likesCount;

            int collectCount = collectService.selectByFidAndModule(fid, LikesModuleEnum.BLOG.getValue());
            userCollectCount += collectCount;
        }
        user.setLikesCount(userLikesCount);
        user.setCollectCount(userCollectCount);

//        查询当前博客的点赞数据
        blog.setUser(user);
        int likesCount = likeService.selectByFidAndModule(id, LikesModuleEnum.BLOG.getValue());
        blog.setLikesCount(likesCount);

        // 查询当前博客的收藏数据
        int collectCount = collectService.selectByFidAndModule(id, LikesModuleEnum.BLOG.getValue());
        blog.setCollectCount(collectCount);

        // 更新博客浏览数据
        blog.setReadCount(blog.getReadCount() + 1);
        this.updateById(blog);
        return blog;
    }

//    查询所有
    @Override
    public List<Blog> selectAll(Blog blog) {
        return blogMapper.selectAll(blog);
    }

//    更新阅读量
    @Override
    public void updateReadCount(Integer blogId) {
        blogMapper.updateReadCount(blogId);
    }

//    查询用户点赞的数据
    @Override
    public PageInfo<Blog> selectLike(Blog blog, Integer pageNum, Integer pageSize) {
        User user = TokenUtils.getCurrentUser();
        blog.setUserId(user.getUserid());
        PageHelper.startPage(pageNum, pageSize);
        List<Blog> list = blogMapper.selectLike(blog);
        PageInfo<Blog> pageInfo = PageInfo.of(list);
        List<Blog> blogList = pageInfo.getList();
        for(Blog b: blogList){
            int likesCount = likeService.selectByFidAndModule(b.getId(), LikesModuleEnum.BLOG.getValue());
            b.setLikesCount(likesCount);
        }
        return pageInfo;
    }

//    查询用户收藏的数据
    @Override
    public PageInfo<Blog> selectCollect(Blog blog, Integer pageNum, Integer pageSize) {
        User user = TokenUtils.getCurrentUser();
        blog.setUserId(user.getUserid());
        PageHelper.startPage(pageNum, pageSize);
        List<Blog> list = blogMapper.selectLike(blog);
        PageInfo<Blog> pageInfo = PageInfo.of(list);
        List<Blog> blogList = pageInfo.getList();
        for(Blog b: blogList){
            int collectCount = likeService.selectByFidAndModule(b.getId(), LikesModuleEnum.BLOG.getValue());
            b.setCollectCount(collectCount);
        }
        return pageInfo;
    }

    @Override
    public PageInfo<Blog> selectPage(Blog blog, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Blog> list = blogMapper.selectAll(blog);
        for(Blog b : list){
            int likesCount = likeService.selectByFidAndModule(b.getId(), LikesModuleEnum.BLOG.getValue());
            b.setCollectCount(likesCount);
        }
        return PageInfo.of(list);
    }

    public PageInfo<Blog> selectUser(Blog blog,Integer pageNum,Integer pageSize){
        User user = TokenUtils.getCurrentUser();
        blog.setUserId(user.getUserid());
        return selectPage(blog,pageNum,pageSize);
    }
}
